package cn.com.dhcc.app.pub.core.util.settlement.compute.store;

import cn.com.dhcc.app.pub.core.util.settlement.compute.configuation.ComputeConfiguation;
import cn.com.dhcc.app.pub.core.util.settlement.compute.filter.DataFilter;
import cn.com.dhcc.app.pub.core.util.settlement.compute.util.HashModUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 默认的数据分组
 * @author Administrator
 *
 */
public class DefaultDataGroupHandler extends ComputeConfiguation implements DataGroupHandler{

	private final int group=DEFAULT_GROUP;
	
	/**
	 * 数据过滤器
	 */
	private List<DataFilter> filters;
	
	public List<DataFilter> getFilters() {
		return filters;
	}
	
	
	public void addFilter(DataFilter filter){
		if (this.filters==null) {
			this.filters=new ArrayList<DataFilter>();
		}
		this.filters.add(filter);
	}
	
	public int getGroup() {
		return group;
	}
	public DefaultDataGroupHandler() {
		super();
		// TODO Auto-generated constructor stub
	}
	/**
	 * 数据过滤处理
	 * @param value
	 * @return
	 */
	private String filter(String value){
		if (filters==null || filters.isEmpty()) {
			return value;
		}
		String result=value;
		for (int i = 0; i < filters.size(); i++) {
			result=filters.get(i).filter(result);
		}
		return result;
	}
	
	/**
	 * 进行数据分组，封装成DataGroupVo对象
	 */
	@Override
	public DataGroupVo group(List<String> datas, int group,boolean mapping) {
		DataGroupVo vo=new DataGroupVo(group,mapping);
		if (datas==null || datas.isEmpty()) {
			return vo;
		}
		for (int i = 0; i < datas.size(); i++) {
			String src=datas.get(i);
			String  value = filter(src);
			int key=HashModUtil.mod(value, group);
			vo.putData(key, value, src);
		}
		return vo;
	}
	@Override
	public DataGroupVo group(List<String> datas,boolean mapping) {
		return group(datas,group,mapping);
	}

	
}
